package com.cz.nongyetongv5.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.cache.ImageLoader;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.bean.New;
import com.cz.nongyetongv5.custom.badgeview.BadgeView;
import com.cz.nongyetongv5.custom.wheelview.Utils;

public class NewsListAdapter extends BaseAdapter {

	// 上下文对象
	private Context context = null;

	private List<New> news = null;

	private boolean mBusy = false;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}

	private ImageLoader mImageLoader;

	public NewsListAdapter(Context context, List<New> news) {
		this.context = context;
		this.news = news;

		this.mImageLoader = new ImageLoader(context);
	}

	@Override
	public int getCount() {
		return news == null ? 0 : news.size();
	}

	@Override
	public Object getItem(int arg0) {
		return news.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {

		ViewGroup jianChaDanWeiLayout = null;

		if (view == null) {
			jianChaDanWeiLayout = (ViewGroup) LayoutInflater.from(context)
					.inflate(R.layout.newslist_pager_item, null);
		} else {
			jianChaDanWeiLayout = (ViewGroup) view;
		}

		New newObj = news.get(position);

		ViewGroup target = (ViewGroup) jianChaDanWeiLayout
				.findViewById(R.id.llNewsItemLayout);

		BadgeView badge = (BadgeView) jianChaDanWeiLayout
				.findViewWithTag("badge");

		if (badge == null) {
			badge = new BadgeView(context, target);
			badge.setTag("badge");
			badge.setText("New!");
			badge.setTextColor(Color.WHITE);
			badge.setBadgeBackgroundColor(Color.RED);
			badge.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
			badge.setBadgeMargin(badge.getLeft() + badge.getWidth(),
					badge.getTop());

			badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
		}

		// 如果是未读
		if (!newObj.isRead()) {
			badge.show();
		} else {

			badge.hide();
		}

		ImageView ivNewsImage = (ImageView) jianChaDanWeiLayout
				.findViewById(R.id.ivNewsImage);

		TextView tvTitle = (TextView) jianChaDanWeiLayout
				.findViewById(R.id.tvTitle);
		tvTitle.setText(newObj.getTitle());

		TextView tvTag = (TextView) jianChaDanWeiLayout
				.findViewById(R.id.tvTag);
		tvTag.setText(newObj.getTag());

		TextView tvDate = (TextView) jianChaDanWeiLayout
				.findViewById(R.id.tvDate);

		tvDate.setText(newObj.getDate());

		TextView tvContent = (TextView) jianChaDanWeiLayout
				.findViewById(R.id.tvContent);
		tvContent.setText(newObj.getContent());

		String imageUrl = newObj.getImageUrl();


		Log.d("image_url", imageUrl + "") ;

		if (imageUrl == null) {
			ivNewsImage.setVisibility(View.GONE);
		} else {

			String fileType = imageUrl.substring(imageUrl.lastIndexOf('.'));

			if (fileType.equalsIgnoreCase(".gif")) {

				ivNewsImage.setVisibility(View.GONE);

			} else {

				ivNewsImage.setVisibility(View.VISIBLE);

				if (!mBusy) {
					mImageLoader.DisplayImage(imageUrl, ivNewsImage, false);
				} else {
					mImageLoader.DisplayImage(imageUrl, ivNewsImage, true);
				}
			}

		}

		return jianChaDanWeiLayout;
	}

	public ImageLoader getmImageLoader() {
		return mImageLoader;
	}

	/**
	 *
	 * @param position
	 *            : 根据已经定位的位置查找下一个未读的定位位置
	 * @return
	 */
	public int getNewIndex(int position) {

		int index = -1;

		int start = index > -1 ? index : 0;

		for (int i = start; i < news.size(); i++) {

			New newObj = news.get(i);

			if (!newObj.isRead() && i > position) {

				index = i + 1;

				break;

			}

		}

		return index;

	}

}
